package com.play.ballen.me.vm

import androidx.lifecycle.MutableLiveData
import com.play.ballen.R
import com.play.ballen.me.entity.MemberIndexInfo
import com.play.ballen.network.ApiRepository
import com.play.ballen.network.BallenRxHelper
import com.yuven.appframework.busevent.SingleLiveEvent
import com.yuven.appframework.network.RxSchedulerHelper
import com.yuven.appframework.util.addToDisposable
import com.yuven.baselib.component.viewmodel.BaseViewModel
import com.yuven.baselib.utils.Logger
import com.yuven.baselib.utils.ResourceUtil

open class UserInfoVM : BaseUserViewModel() {

    val memberIndexInfo = MutableLiveData<MemberIndexInfo>()
    val actionEditSuccess = SingleLiveEvent<Void>()

    fun getMemberIndex() {
        model.getMemberIndex()
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .subscribe({
                Logger.i("MeFragmentVM.getMemberIndex success")
                memberIndexInfo.value = it
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("MeFragmentVM.getMemberIndex error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

    fun memberEdit(name: String) {
        showDialog.setValue(true, ResourceUtil.getString(R.string.loading))
        val map = mutableMapOf<String, Any>()
        map["nick_name"] = name
        model.memberEdit(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .doFinally {
                showDialog.setValue(false)
            }
            .subscribe({
                Logger.i("UserInfoVM.memberEdit success")
                actionEditSuccess.call()
            }, { throwable ->
                toastLiveData.value = throwable.message
                Logger.i("UserInfoVM.memberEdit error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }


}